import os;
import time;
import requests as req;
import lxml;
from bs4 import BeautifulSoup;

def get_list(url,params,header,name):



    file_name = "./bug_file/炉石传说/"+name;

    if not os.path.exists(file_name):
        os.makedirs(file_name);


    result = req.get(url=url, params=params, headers=header);


    html_text = result.json();
    # html_text = html_text.encode("ISO-8859-1");
    # html_text = html_text.decode("utf-8");

    # bs_obj = BeautifulSoup(html_text,'lxml');
    # images = bs_obj.select("#cards_box .card_img");

    total = html_text.get("total");

    for item in html_text.get("cards"):
        src = item.get("pic");

        image = req.get(url=src);

        io = image.content;
        with open(file_name+"/$"+str(item.get("cost"))+"_"+item.get("name")+".png","wb") as wf:
            wf.write(io);

        print(name+"-"+item.get("name")+"\t下载成功!!");

    return total;




if __name__ == '__main__':

    start_time = time.time();
    total_count = 0;

    url = "https://hs.blizzard.cn/action/cards/query";

    clazz = [
        {"name":"恶魔猎手","class":"demonhunter"},
        {"name":"德鲁伊","class":"druid"},
        {"name":"猎人","class":"hunter"},
        {"name":"法师","class":"mage"},
        {"name":"圣骑士","class":"paladin"},
        {"name":"牧师","class":"priest"},
        {"name":"盗贼","class":"rogue"},
        {"name":"萨满","class":"shaman"},
        {"name":"术士","class":"warlock"},
        {"name":"战士","class":"warrior"},
        {"name":"中立","class":"neutral"}
    ]

    # UA伪装
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
        "Mobile-User-Agent": "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36"
    }

    for clazz_item in clazz:

        clazz_clazz = clazz_item.get("class");
        clazz_name = clazz_item.get("name");

        print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+f"开始爬取-{clazz_item.get('name')}卡牌"+"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");

        for index in range(2000):

            params = {
                "cardClass": clazz_clazz,
                "cost": "", # 费用,不传为所有
                "keywords": "",
                "standard": 0, # 0狂野 1标准
                "t": time.time(),
                "cardSet": "",
                "p": index,
            }


            total = get_list(url,params,header,clazz_name);

            if index * 8 > int(total):
                total_count += total;
                print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+clazz_name + "\t卡牌爬取完毕,共爬取"+str(total)+"张"+"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
                break;
            else:
                continue;

    end_time = time.time();
    print(f">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>本次爬取结束,用时{end_time - start_time},共爬取数据{str(total_count)}<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");